﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TV4Home.Server.IssueReporting;
using TV4Home.WebInterface.UI.Code;

namespace TV4Home.WebInterface.UI
{
    public partial class BugReport : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                // initialize description textbox
                txtDescription.Attributes.Add("maxlength", "1500");
                txtDescription.Text = String.Format(CultureInfo.CurrentCulture,
                                                    "{0}{0}{0}{0}{0}{0}----- General information ----- {0}Web-Interface Version: {1}{0}Browser UserAgent: {2}{0}Browser Accept-Languages: {3}{0}Webserver OS: {4}{0}ASP.NET Version: {5}{0}",
                                                    Environment.NewLine,
                                                    Utils.Misc.WebsiteVersion,
                                                    Request.UserAgent,
                                                    Request.UserLanguages != null ? String.Join(";", Request.UserLanguages) : "",
                                                    Environment.OSVersion,
                                                    Environment.Version);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string issueId = SubmitBugReport();

                this.lblIssueID.Text = issueId;
                this.hlWorkItemLink.NavigateUrl = String.Format(this.hlWorkItemLink.NavigateUrl, issueId);
                this.hlWorkItemLink.Text = this.hlWorkItemLink.NavigateUrl;
            }
            catch (Exception ex)
            {
                string message = "Error: " + ex.Message;

                if (ex.InnerException != null)
                    message += ex.InnerException.Message;

                lblError.Text = message;

                pnError.Visible = true;
                pnSuccess.Visible = false;
            }

            MultiViewMain.SetActiveView(ThankYouView);
        }

        private string SubmitBugReport()
        {
            // input validation
            if (String.IsNullOrEmpty(txtTitle.Text) || txtTitle.Text.Length < 3 || txtTitle.Text.Length > 80)
                throw new Exception("Title must be at least 3 characters long and mustn't exceed 80 characters!"); //TODO: localize exception

            if (String.IsNullOrEmpty(txtDescription.Text) || txtDescription.Text.Length < 10 || txtDescription.Text.Length > 1500)
                throw new Exception("Please enter between 10 and 1500 characters for the description!"); //TODO: localize exception

            if ((fuAttachment1.HasFile && fuAttachment1.PostedFile.ContentLength > 350000) ||
                (fuAttachment2.HasFile && fuAttachment2.PostedFile.ContentLength > 350000) ||
                (fuAttachment3.HasFile && fuAttachment3.PostedFile.ContentLength > 350000) ||
                (fuAttachment4.HasFile && fuAttachment4.PostedFile.ContentLength > 350000))
                throw new Exception("One of the attachments is larger than 350KB!"); //TODO: localize exception

            // create issue object
            WebIssue issue = new WebIssue
                                 {
                                     Title = String.Format("[{0}] {1}", rbIssue.Checked ? "BUG" : "FRQ", txtTitle.Text),
                                     Description = txtDescription.Text,
                                     WorkItemType = rbIssue.Checked ? "Issue" : "Feature",
                                     Component = @"TV4Home\Web-Interface",
                                     Priority = new[] { "Low", "Medium", "High" }[Convert.ToInt32(rblPriority.SelectedValue)]
                                 };

            // add attachments
            if (fuAttachment1.HasFile) issue.AddAttachment(fuAttachment1.FileBytes, fuAttachment1.FileName);
            if (fuAttachment2.HasFile) issue.AddAttachment(fuAttachment2.FileBytes, fuAttachment2.FileName);
            if (fuAttachment3.HasFile) issue.AddAttachment(fuAttachment3.FileBytes, fuAttachment3.FileName);
            if (fuAttachment4.HasFile) issue.AddAttachment(fuAttachment4.FileBytes, fuAttachment4.FileName);

            // report the issue
            new IssueReporting().UploadIssue(issue, false);

            return issue.IssueId.ToString();
        }
    }
}
